package action;

import bean.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;
/**
 * Created by hadoop on 17-6-16.
 * 处理LoginForm.jsp的表单action
 */
public class LoginAction extends ActionSupport{
    private String username;
    private String password;
    private User user;
    private Map<String, User> users;
    private String targetPage;

    //用来处理登录请求
    @Override
    public String execute() throws Exception {
        //为了直接请求/login返回表单页面，类似于django
        if(!check()){
            return "index";
        }
        ActionContext ctx = ActionContext.getContext();

        //添加计数到application
        Integer counter = (Integer) ctx.getApplication().get("counter");
        counter = counter == null ? 1 : counter+1;
        ctx.getApplication().put("counter",counter);

        //给action添加PreResultListener监听器，在转入实际物理视图之前回调
        ActionInvocation invocation =  ctx.getActionInvocation();
        //lambda表达式实现PreResultListener接口的beforeResult方法
        invocation.addPreResultListener((invocat,resultCode)->{
            System.out.println("返回的逻辑视图名为: "+resultCode);
            invocat.getInvocationContext().put("extra","由"+resultCode+"逻辑视图名转入");
        });
        //判断输入
        if(getUser().getName().equals("1")&&getUser().getPass().equals("1")){
            ctx.getSession().put("user",getUser().getName());
            //使用Action国际化
            ctx.put("tip",getText("succTip",new String[]{getUser().getName()}));
            return SUCCESS;
        }
        ctx.put("tip",getText("failTip"));
        return ERROR;
    }
    //用来显示表单页面
//    public String index() throws Exception{
//        return INPUT;
//    }

    //用来判断输入是否为空,这样直接在浏览器访问/login就会跳转到LoginForm.jsp页面
    private boolean check(){
        if (getUser()==null){
            return false;
        }
        return true;
    }

    //用来处理注册请求
    public String register() throws Exception{
        ActionContext ctx = ActionContext.getContext();
        ctx.put("tip","register success");
        ctx.getSession().put("user",getUsername());
        return SUCCESS;
    }

    //处理重定向的请求
    public String redirect() throws Exception{
        return SUCCESS;
    }

    //测试基于ONGL的类型转换
    public String execute2() throws Exception{
        if(getUsers()==null){
            return "index";
        }
        for(String k : getUsers().keySet()){
            addActionMessage(k+"=>"+getUsers().get(k).getName()+";"+getUsers().get(k).getPass());
        }
        return SUCCESS;
    }

    //setter and getter
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTargetPage() {
        return targetPage;
    }

    public void setTargetPage(String targetPage) {
        this.targetPage = targetPage;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Map<String, User> getUsers() {
        return users;
    }

    public void setUsers(Map<String, User> users) {
        this.users = users;
    }
}
